package com.ying.user.controller;

import cn.hutool.core.collection.CollUtil;
import com.ying.common.entity.Response;
import com.ying.common.entity.ResponseCode;
import com.ying.common.exception.SecurityException;
import com.ying.user.service.MonitorService;
import com.ying.user.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/monitor")
public class MonitorController {
    @Autowired
    private MonitorService monitorService;

    /**
     * 在线用户列表
     */
    @GetMapping("/onlineUser")
    public Response onlineUser(Integer pageNum, Integer pageSize) {
        Response response = monitorService.onlineUser(pageNum, pageSize);
        return response;
    }

    /**
     * 批量踢出在线用户
     *
     * @param names 用户名列表
     */
    @DeleteMapping("/onlineUser/kickOut")
    public Response kickOutOnlineUser(@RequestBody List<String> names) {
        if (CollUtil.isEmpty(names)) {
            throw new SecurityException(ResponseCode.BAD_REQUEST);
        }
        if (names.contains(SecurityUtils.getCurrentUsername())){
            throw new SecurityException(ResponseCode.KICKOUT_SELF);
        }
        monitorService.kickOut(names);
        return new Response().success();
    }
}
